﻿using System;
using Vbyte.SharpOrm.SqlDb;

namespace Vbyte.SharpOrm.Config
{
    /// <summary>
    /// 导入数据表映射属性时可忽略的字段
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public class ImportIgnoreAttribute : Attribute
    {

    }

    /// <summary>
    /// SQL联合查询时忽略设置
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public class LinkIgnoreAttribute : Attribute
    {

    }

    /// <summary>
    /// 操作命令(数据库交互)可忽略的字段
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public class CommandIgnoreAttribute : Attribute
    {

    }

    /// <summary>
    /// SQL数据库主键属性设置
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public class PrimaryKeyAttribute : Attribute
    {
        /// <summary>
        /// 默认配置一个不能插入的主键设置
        /// </summary>
        public PrimaryKeyAttribute()
            : this(false)
        { }

        /// <summary>
        /// 配置主键是否能支持Insert
        /// </summary>
        /// <param name="isInsertable">是否支持Insert</param>
        public PrimaryKeyAttribute(bool isInsertable)
        {
            InsertAble = isInsertable;
        }

        /// <summary>
        /// 该主键是否支持手工插入新值（默认否)
        /// </summary>
        public bool InsertAble { get; set; }
    }

    /// <summary>
    /// SQL数据库标志属性设置
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public class IdentityAttribute : Attribute
    {
    }

    /// <summary>
    /// SQL数据库自动增长属性设置
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public class AutoIncrementAttribute : Attribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AutoIncrementAttribute"/> class.
        /// </summary>
        /// <param name="seed">The seed.</param>
        /// <param name="step">The step.</param>
        public AutoIncrementAttribute(int seed, int step)
        {
            autoIncrement = new SqlAutoIncrement(seed, step);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AutoIncrementAttribute"/> class.
        /// </summary>
        public AutoIncrementAttribute()
        {
            autoIncrement = new SqlAutoIncrement(1, 1);
        }

        private SqlAutoIncrement autoIncrement;

        /// <summary>
        /// 获取自动增长的配置
        /// </summary>
        /// <returns></returns>
        public SqlAutoIncrement GetIncrementConfig()
        {
            return autoIncrement;
        }
    }


    /// <summary>
    /// 对该字段建立索引标记
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public class IndexedAttribute : Attribute
    {
        /// <summary>
        /// 创建默认非聚焦、可重复的索引
        /// </summary>
        public IndexedAttribute()
        {
            IndexConfig = new SqlIndexConfig();
        }

        /// <summary>
        /// 创建指定聚焦、重复与否的索引
        /// </summary>
        /// <param name="isUNIQUE">是否不重复</param>
        /// <param name="isCLUSTERED">是否聚焦</param>
        public IndexedAttribute(bool isUNIQUE, bool isCLUSTERED)
        {
            IndexConfig = new SqlIndexConfig(isUNIQUE, isCLUSTERED);
        }

        /// <summary>
        /// 创建指定聚焦、重复与否的索引
        /// </summary>
        /// <param name="idxName">索引名称</param>
        /// <param name="isUNIQUE">是否不重复</param>
        /// <param name="isCLUSTERED">是否聚焦</param>
        public IndexedAttribute(string idxName, bool isUNIQUE, bool isCLUSTERED)
        {
            IndexConfig = new SqlIndexConfig(idxName, isUNIQUE, isCLUSTERED);
        }

        /// <summary>
        /// 创建指定聚焦、重复与否的索引
        /// </summary>
        /// <param name="idxName">索引名称</param>
        /// <param name="isUNIQUE">是否不重复</param>
        /// <param name="isCLUSTERED">是否聚焦</param>
        /// <param name="isDesc">建立倒序索引</param>
        public IndexedAttribute(string idxName, bool isUNIQUE, bool isCLUSTERED, bool isDesc)
        {
            IndexConfig = new SqlIndexConfig(idxName, isUNIQUE, isCLUSTERED, isDesc);
        }

        /// <summary>
        /// 创建指定聚焦、重复与否的索引
        /// </summary>
        /// <param name="isUNIQUE">是否不重复</param>
        /// <param name="isCLUSTERED">是否聚焦</param>
        /// <param name="isDesc">建立倒序索引</param>
        public IndexedAttribute(bool isUNIQUE, bool isCLUSTERED, bool isDesc)
        {
            SqlIndexConfig config = new SqlIndexConfig();
            config.IsClustered = isCLUSTERED;
            config.IsUnique = isUNIQUE;
            config.IsDescIndex = isDesc;

            IndexConfig = config;
        }

        /// <summary>
        /// 获取或设置索引配置
        /// </summary>
        public SqlIndexConfig IndexConfig { get; set; }

    }

}